package co.fitstart.admin.web.controller.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.ReportService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.course.Course;
import co.fitstart.entity.course.CourseUnit;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.round.CourseRoundResult;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.StudentTargetData;

/**
 * 结营学员报表
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/stop-camp-reports")
@MenuSetting("menu-report")
public class StopCampReportController {
    
    @Autowired
    private SiteService siteService;
    
    @Autowired
    private ReportService reportService;
    
    @Autowired
    private PrincipalService principalService;
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-stop-camp-report")
    public ModelAndView list(@Secure Login login,
                             @RequestParam(name = "site", required = false) Long siteId,
                             @RequestParam(name = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                             @RequestParam(name = "period", required = false, defaultValue = "day") String period) {
        
        ModelAndView modelAndView = new ModelAndView("report/stop-camp-report-list");
        
        Site site = null;
        if(login.hasSite()) {
            site = login.getSite();
        } else if(siteId != null) {
            site = siteService.load(siteId);
        }
        
        List<Site> sites = new ArrayList<>();
        
        if(date == null) {
            date = new Date();
        }
        
        Date begin = date;
        Date end = date;
        
        if("week".equals(period)) {
            begin = DateUtils.parseDate(DateFormatUtils.format(DateUtils.getFirstWeekDay(date), "yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
            end = DateUtils.parseDate(DateFormatUtils.format(DateUtils.getLastWeekDay(date), "yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
        } else if("month".equals(period)) {
            begin = DateUtils.parseDate(DateFormatUtils.format(DateUtils.getFirstMonthDay(date), "yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
            end = DateUtils.parseDate(DateFormatUtils.format(DateUtils.getLastMonthDay(date), "yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
        }
        
        int totalAmount = 0;
        List<CourseRound> courseRoundList = new ArrayList<>();
        
        if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                if(siteId == null) {
                    totalAmount = reportService.countFinishedCourse(begin, end, sites);
                    courseRoundList = reportService.listFinishedCourseRound(begin, end, sites);
                } else if(site != null && sites.contains(site)) {
                    totalAmount = reportService.countFinishedCourse(begin, end, site);
                    courseRoundList = reportService.listFinishedCourseRound(begin, end, site);
                }
            }
        } else {
            totalAmount = reportService.countFinishedCourse(begin, end, site);
            courseRoundList = reportService.listFinishedCourseRound(begin, end, site);
        }
        
        Map<Long, Map<Long, Long>> result = new HashMap<>();
        
        int individualCount = 0;
        int specialCount = 0;
        int postureCount = 0;
        int yearCount = 0;
        
        if(site == null) {
            if(!login.isAreaManager()) {
                sites = siteService.loadAll();
            }
            
            for(Site eachSite : sites) {
            	
            	
                Map<Long, Long> items = new HashMap<Long, Long>();
                items.put(new Long(1), new Long(0));
                items.put(new Long(2), new Long(0));
                items.put(new Long(3), new Long(0));
                items.put(new Long(4), new Long(0));
                items.put(new Long(5), new Long(0));
                items.put(new Long(6), new Long(0));
                items.put(new Long(7), new Long(0));
                items.put(new Long(8), new Long(0));
                items.put(new Long(9), new Long(0));
                items.put(new Long(10), new Long(0));
                items.put(new Long(11), new Long(0));
                items.put(new Long(12), new Long(0));
                items.put(new Long(13), new Long(0));
                items.put(new Long(14), new Long(0));
                items.put(new Long(15), new Long(0));
                items.put(new Long(16), new Long(0));
                items.put(new Long(17), new Long(0));
                items.put(new Long(18), new Long(0));
                items.put(new Long(19), new Long(0));
                items.put(new Long(20), new Long(0));
                items.put(new Long(21), new Long(0));
                items.put(new Long(22), new Long(0));
                items.put(new Long(23), new Long(0));
                items.put(new Long(24), new Long(0));
                items.put(new Long(25), new Long(0));
                items.put(new Long(26), new Long(0));
                items.put(new Long(27), new Long(0));
                items.put(new Long(28), new Long(0));
                items.put(new Long(29), new Long(0));
                items.put(new Long(30), new Long(0));
                items.put(new Long(31), new Long(0));
                items.put(new Long(32), new Long(0));
                items.put(new Long(33), new Long(0));
                items.put(new Long(34), new Long(0));
                items.put(new Long(35), new Long(0));
                items.put(new Long(36), new Long(0));
                items.put(new Long(37), new Long(0));
                items.put(new Long(38), new Long(0));
                items.put(new Long(39), new Long(0));
                items.put(new Long(40), new Long(0));
                
                if(!eachSite.isDisabled()) {
                    for(CourseRound courseRound : courseRoundList) {
                        if(courseRound.getSite() == eachSite) {
                            if(courseRound.getCourse().getCourseUnit().equals(CourseUnit.NORMAL_INDIVIDUAL) ||
                                    courseRound.getCourse().getCourseUnit().equals(CourseUnit.OTHER_INDIVIDUAL) ||
                                    courseRound.getCourse().getCourseUnit().equals(CourseUnit.NEW_OTHER_INDIVIDUAL)) {
                                Long count = items.get(new Long(1))+1;
                                items.put(new Long(1), count);
                                individualCount = individualCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(2))+1;
                                    items.put(new Long(2), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.FAST_INDIVIDUAL)) {
                                Long count = items.get(new Long(3))+1;
                                items.put(new Long(3), count);
                                individualCount = individualCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(4))+1;
                                    items.put(new Long(4), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SHAPE_INDIVIDUAL)) {
                                Long count = items.get(new Long(5))+1;
                                items.put(new Long(5), count);
                                individualCount = individualCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(6))+1;
                                    items.put(new Long(6), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.YOUNG_INDIVIDUAL)) {
                                Long count = items.get(new Long(7))+1;
                                items.put(new Long(7), count);
                                individualCount = individualCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(8))+1;
                                    items.put(new Long(8), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.Y_BOXING_SPECIAL) || courseRound.getCourse().getCourseUnit().equals(CourseUnit.BOXING_SPECIAL)) {
                                Long count = items.get(new Long(9))+1;
                                items.put(new Long(9), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(10))+1;
                                    items.put(new Long(10), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.Y_HANG_TRAINING)) {
                                Long count = items.get(new Long(11))+1;
                                items.put(new Long(11), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(12))+1;
                                    items.put(new Long(12), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.H_STRETCH_SPECIAL)) {
                                Long count = items.get(new Long(13))+1;
                                items.put(new Long(13), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(14))+1;
                                    items.put(new Long(14), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.BABY_SPECIAL)) {
                                Long count = items.get(new Long(15))+1;
                                items.put(new Long(15), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(16))+1;
                                    items.put(new Long(16), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.VIPR_SPECIAL)) {
                                Long count = items.get(new Long(17))+1;
                                items.put(new Long(17), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(18))+1;
                                    items.put(new Long(18), ccount);
                                }
                            }  else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.BODY_SPECIAL)) {
                                Long count = items.get(new Long(19))+1;
                                items.put(new Long(19), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(20))+1;
                                    items.put(new Long(20), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SPECIAL_UNION)) {
                                Long count = items.get(new Long(21))+1;
                                items.put(new Long(21), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(22))+1;
                                    items.put(new Long(22), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SPECIAL_ALL)) {
                                Long count = items.get(new Long(39))+1;
                                items.put(new Long(39), count);
                                specialCount = specialCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(40))+1;
                                    items.put(new Long(40), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.NORMAL_POSTURE) ||
                                    courseRound.getCourse().getCourseUnit().equals(CourseUnit.POSTURE_BEAUTY)) {
                                Long count = items.get(new Long(23))+1;
                                items.put(new Long(23), count);
                                postureCount = postureCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(24))+1;
                                    items.put(new Long(24), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.POSTURE_FIX)) {
                                Long count = items.get(new Long(25))+1;
                                items.put(new Long(25), count);
                                postureCount = postureCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(26))+1;
                                    items.put(new Long(26), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.YEAR_ROUND)) {
                                Long count = items.get(new Long(27))+1;
                                items.put(new Long(27), count);
                                yearCount = yearCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(28))+1;
                                    items.put(new Long(28), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.MONTH_ROUND)) {
                                Long count = items.get(new Long(29))+1;
                                items.put(new Long(29), count);
                                yearCount = yearCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(30))+1;
                                    items.put(new Long(30), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SEASON_ROUND)) {
                                Long count = items.get(new Long(31))+1;
                                items.put(new Long(31), count);
                                yearCount = yearCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(32))+1;
                                    items.put(new Long(32), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.YEAR_SPECIAL_ROUND)) {
                                Long count = items.get(new Long(33))+1;
                                items.put(new Long(33), count);
                                yearCount = yearCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(34))+1;
                                    items.put(new Long(34), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.MONTH_SPECIAL_ROUND)) {
                                Long count = items.get(new Long(35))+1;
                                items.put(new Long(35), count);
                                yearCount = yearCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(36))+1;
                                    items.put(new Long(36), ccount);
                                }
                            } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SEASON_SPECIAL_ROUND)) {
                                Long count = items.get(new Long(37))+1;
                                items.put(new Long(37), count);
                                yearCount = yearCount + 1;
                                if(!courseRound.getLastCourse()) {
                                    Long ccount = items.get(new Long(38))+1;
                                    items.put(new Long(38), ccount);
                                }
                            } 
                        }
                    }
                    result.put(eachSite.getId(), items);
                }
            }
        } else {
            
        	Map<Long, Long> items = new HashMap<Long, Long>();
            items.put(new Long(1), new Long(0));
            items.put(new Long(2), new Long(0));
            items.put(new Long(3), new Long(0));
            items.put(new Long(4), new Long(0));
            items.put(new Long(5), new Long(0));
            items.put(new Long(6), new Long(0));
            items.put(new Long(7), new Long(0));
            items.put(new Long(8), new Long(0));
            items.put(new Long(9), new Long(0));
            items.put(new Long(10), new Long(0));
            items.put(new Long(11), new Long(0));
            items.put(new Long(12), new Long(0));
            items.put(new Long(13), new Long(0));
            items.put(new Long(14), new Long(0));
            items.put(new Long(15), new Long(0));
            items.put(new Long(16), new Long(0));
            items.put(new Long(17), new Long(0));
            items.put(new Long(18), new Long(0));
            items.put(new Long(19), new Long(0));
            items.put(new Long(20), new Long(0));
            items.put(new Long(21), new Long(0));
            items.put(new Long(22), new Long(0));
            items.put(new Long(23), new Long(0));
            items.put(new Long(24), new Long(0));
            items.put(new Long(25), new Long(0));
            items.put(new Long(26), new Long(0));
            items.put(new Long(27), new Long(0));
            items.put(new Long(28), new Long(0));
            items.put(new Long(29), new Long(0));
            items.put(new Long(30), new Long(0));
            items.put(new Long(31), new Long(0));
            items.put(new Long(32), new Long(0));
            items.put(new Long(33), new Long(0));
            items.put(new Long(34), new Long(0));
            items.put(new Long(35), new Long(0));
            items.put(new Long(36), new Long(0));
            items.put(new Long(37), new Long(0));
            items.put(new Long(38), new Long(0));
            items.put(new Long(39), new Long(0));
            items.put(new Long(40), new Long(0));
            
            for(CourseRound courseRound : courseRoundList) {
                
            	if(courseRound.getCourse().getCourseUnit().equals(CourseUnit.NORMAL_INDIVIDUAL) ||
                        courseRound.getCourse().getCourseUnit().equals(CourseUnit.OTHER_INDIVIDUAL) ||
                        courseRound.getCourse().getCourseUnit().equals(CourseUnit.NEW_OTHER_INDIVIDUAL)) {
                    Long count = items.get(new Long(1))+1;
                    items.put(new Long(1), count);
                    individualCount = individualCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(2))+1;
                        items.put(new Long(2), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.FAST_INDIVIDUAL)) {
                    Long count = items.get(new Long(3))+1;
                    items.put(new Long(3), count);
                    individualCount = individualCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(4))+1;
                        items.put(new Long(4), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SHAPE_INDIVIDUAL)) {
                    Long count = items.get(new Long(5))+1;
                    items.put(new Long(5), count);
                    individualCount = individualCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(6))+1;
                        items.put(new Long(6), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.YOUNG_INDIVIDUAL)) {
                    Long count = items.get(new Long(7))+1;
                    items.put(new Long(7), count);
                    individualCount = individualCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(8))+1;
                        items.put(new Long(8), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.Y_BOXING_SPECIAL) || courseRound.getCourse().getCourseUnit().equals(CourseUnit.BOXING_SPECIAL)) {
                    Long count = items.get(new Long(9))+1;
                    items.put(new Long(9), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(10))+1;
                        items.put(new Long(10), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.Y_HANG_TRAINING)) {
                    Long count = items.get(new Long(11))+1;
                    items.put(new Long(11), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(12))+1;
                        items.put(new Long(12), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.H_STRETCH_SPECIAL)) {
                    Long count = items.get(new Long(13))+1;
                    items.put(new Long(13), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(14))+1;
                        items.put(new Long(14), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.BABY_SPECIAL)) {
                    Long count = items.get(new Long(15))+1;
                    items.put(new Long(15), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(16))+1;
                        items.put(new Long(16), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.VIPR_SPECIAL)) {
                    Long count = items.get(new Long(17))+1;
                    items.put(new Long(17), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(18))+1;
                        items.put(new Long(18), ccount);
                    }
                }  else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.BODY_SPECIAL)) {
                    Long count = items.get(new Long(19))+1;
                    items.put(new Long(19), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(20))+1;
                        items.put(new Long(20), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SPECIAL_UNION)) {
                    Long count = items.get(new Long(21))+1;
                    items.put(new Long(21), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(22))+1;
                        items.put(new Long(22), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SPECIAL_ALL)) {
                    Long count = items.get(new Long(39))+1;
                    items.put(new Long(39), count);
                    specialCount = specialCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(40))+1;
                        items.put(new Long(40), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.NORMAL_POSTURE) ||
                        courseRound.getCourse().getCourseUnit().equals(CourseUnit.POSTURE_BEAUTY)) {
                    Long count = items.get(new Long(23))+1;
                    items.put(new Long(23), count);
                    postureCount = postureCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(24))+1;
                        items.put(new Long(24), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.POSTURE_FIX)) {
                    Long count = items.get(new Long(25))+1;
                    items.put(new Long(25), count);
                    postureCount = postureCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(26))+1;
                        items.put(new Long(26), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.YEAR_ROUND)) {
                    Long count = items.get(new Long(27))+1;
                    items.put(new Long(27), count);
                    yearCount = yearCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(28))+1;
                        items.put(new Long(28), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.MONTH_ROUND)) {
                    Long count = items.get(new Long(29))+1;
                    items.put(new Long(29), count);
                    yearCount = yearCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(30))+1;
                        items.put(new Long(30), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SEASON_ROUND)) {
                    Long count = items.get(new Long(31))+1;
                    items.put(new Long(31), count);
                    yearCount = yearCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(32))+1;
                        items.put(new Long(32), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.YEAR_SPECIAL_ROUND)) {
                    Long count = items.get(new Long(33))+1;
                    items.put(new Long(33), count);
                    yearCount = yearCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(34))+1;
                        items.put(new Long(34), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.MONTH_SPECIAL_ROUND)) {
                    Long count = items.get(new Long(35))+1;
                    items.put(new Long(35), count);
                    yearCount = yearCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(36))+1;
                        items.put(new Long(36), ccount);
                    }
                } else if (courseRound.getCourse().getCourseUnit().equals(CourseUnit.SEASON_SPECIAL_ROUND)) {
                    Long count = items.get(new Long(37))+1;
                    items.put(new Long(37), count);
                    yearCount = yearCount + 1;
                    if(!courseRound.getLastCourse()) {
                        Long ccount = items.get(new Long(38))+1;
                        items.put(new Long(38), ccount);
                    }
                }
            }
            result.put(site.getId(), items);
        }
        
        modelAndView.addObject("selectSite", site);
        modelAndView.addObject("day", DateFormatUtils.format(date, "yyyy-MM-dd"));
        modelAndView.addObject("period", period);
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        modelAndView.addObject("totalAmount", totalAmount);
        modelAndView.addObject("individualCount", individualCount);
        modelAndView.addObject("specialCount", specialCount);
        modelAndView.addObject("postureCount", postureCount);
        modelAndView.addObject("yearCount", yearCount);
        modelAndView.addObject("result", result);
        modelAndView.addObject("courseRoundList", courseRoundList);
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("report/stop-camp-report-export");
        
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param siteId
     * @param beginDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login,
                                         @RequestParam(value = "site", required=false) Long siteId,
                                         @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                         @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("stop-camp-report.xls"));
        
        Site site = null;
        if(!login.hasSite() && siteId != null) {
            site = siteService.load(siteId);
        }else if (login.hasSite()){
            site = login.getSite();
        }
        
        if(beginDate == null || endDate == null) {
            beginDate = new Date();
            endDate = new Date();
        }
        
        List<Site> sites = new ArrayList<>();
        
        int totalAmount = 0;
        List<CourseRound> courseRoundList = new ArrayList<>();
        Map<Long, Long> multipleOrderUsers = new HashMap<>();
        List<CourseRoundResult> courseRoundResults = new ArrayList<>();
        Map<Long, Long> multipleOrders = new HashMap<>();
        Map<Long, Long> finishedStudents = new HashMap<>();
        List<StudentTargetData> targets = new ArrayList<>();
        
        if(site == null && (login.isAreaManager() || login.isAreaServer())) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                totalAmount = reportService.countFinishedCourse(beginDate, endDate, sites);
                courseRoundList = reportService.listFinishedCourseRound(beginDate, endDate, sites);
                courseRoundResults = reportService.listCourseRoundResult(beginDate, endDate, sites);
                multipleOrderUsers = reportService.listMultipleOrder(beginDate, endDate, sites);
                multipleOrders = reportService.countMultipleOrderBySite(beginDate, endDate, sites);
                finishedStudents = reportService.countFinishedStudent(beginDate, endDate, sites);
                targets = reportService.listStudentTargetData(beginDate, endDate, sites);
            }
        } else {
            totalAmount = reportService.countFinishedCourse(beginDate, endDate, site);
            courseRoundList = reportService.listFinishedCourseRound(beginDate, endDate, site);
            courseRoundResults = reportService.listCourseRoundResult(beginDate, endDate, site);
            multipleOrderUsers = reportService.listMultipleOrder(beginDate, endDate, site);
            multipleOrders = reportService.countMultipleOrderBySite(beginDate, endDate, site);
            finishedStudents = reportService.countFinishedStudent(beginDate, endDate, site);
            targets = reportService.listStudentTargetData(beginDate, endDate, site);
        }
        
        List<Course> courseList = reportService.listOfflineCourse();
        
        Map<Long, Map<Long, Long>> result = new HashMap<>();
        
        
        if(site == null) {
            if(!login.isAreaManager() && !login.isAreaServer()) {
                sites = siteService.loadAll();
            }
            
            for(Site eachSite : sites) {
                Map<Long, Long> map = reportService.listFinishedCourseCount(beginDate, endDate, eachSite);
                result.put(eachSite.getId(), map);
            }
            
            modelAndView.addObject("allSiteId", 0l);
        } else {
            sites.add(site);
            
            Map<Long, Long> map = reportService.listFinishedCourseCount(beginDate, endDate, site);
            result.put(site.getId(), map);
        }
        
        modelAndView.addObject("beginDate", DateFormatUtils.format(beginDate, "yyyy-MM-dd"));
        modelAndView.addObject("endDate", DateFormatUtils.format(endDate, "yyyy-MM-dd"));
        modelAndView.addObject("totalAmount", totalAmount);
        modelAndView.addObject("sites", sites);
        modelAndView.addObject("courseList", courseList);
        modelAndView.addObject("result", result);
        modelAndView.addObject("courseRoundList", courseRoundList);
        modelAndView.addObject("multipleOrderUsers", multipleOrderUsers);
        modelAndView.addObject("courseRoundResults", CollectionUtils.constructMapValueFromList(courseRoundResults, "courseRound.id"));
        modelAndView.addObject("multipleOrders", multipleOrders);
        modelAndView.addObject("finishedStudents", finishedStudents);
        modelAndView.addObject("targets", CollectionUtils.constructMapValueFromList(targets, "courseRound.id"));
        
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
    }
}
